package com.cloud.picture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.picture.annotation.AuthCheck;
import com.cloud.picture.common.BaseResponse;
import com.cloud.picture.constant.UserConstants;
import com.cloud.picture.model.dto.DeleteRequest;
import com.cloud.picture.model.dto.space.SpaceAddRequest;
import com.cloud.picture.model.dto.space.SpaceEditRequest;
import com.cloud.picture.model.dto.space.SpaceQueryRequest;
import com.cloud.picture.model.dto.space.SpaceUpdateRequest;
import com.cloud.picture.model.entity.Space;
import com.cloud.picture.model.entity.User;
import com.cloud.picture.model.enums.ErrorCodeEnum;
import com.cloud.picture.model.enums.SpaceLevelEnum;
import com.cloud.picture.model.vo.SpaceLevelVO;
import com.cloud.picture.model.vo.SpaceVO;
import com.cloud.picture.service.SpaceService;
import com.cloud.picture.service.UserService;
import com.cloud.picture.utils.ResultUtils;
import com.cloud.picture.utils.ThrowUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author syf
 */
@RestController
@Slf4j
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private SpaceService spaceService;
    @Resource
    private UserService userService;

    /**
     * 创建空间
     * @param spaceAddRequest 请求体
     * @return 是否创建成功
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建空间")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAddRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Long res = spaceService.addSpace(spaceAddRequest, loginUser);
        return ResultUtils.success(res);
    }

    /**
     * 删除空间
     * @param deleteRequest 请求体
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除空间")
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest , HttpServletRequest request) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCodeEnum.PARAMS_ERROR);
        boolean res = spaceService.deleteSpace(deleteRequest, request);
        return ResultUtils.success(res);
    }

    /**
     * 更新空间（仅管理员）
     * @param spaceUpdateRequest 请求体
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "更新空间")
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest) {
        ThrowUtils.throwIf(spaceUpdateRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        return ResultUtils.success(spaceService.updateSpace(spaceUpdateRequest));
    }

    /**
     * 编辑空间
     * @param spaceEditRequest 请求体
     * @return 是否编辑成功
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑空间")
    public BaseResponse<Boolean> editSpace(@RequestBody SpaceEditRequest spaceEditRequest) {
        ThrowUtils.throwIf(spaceEditRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        return ResultUtils.success(spaceService.editSpace(spaceEditRequest));
    }

    /**
     * 根据空间id获取空间
     * @param id id
     * @param request 请求
     * @return 空间
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "根据id获取空间")
    public BaseResponse<Space> getSpaceById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCodeEnum.PARAMS_ERROR);
        Space space = spaceService.getById(id);
        ThrowUtils.throwIf(space == null, ErrorCodeEnum.NOT_FOUND_ERROR);
        return ResultUtils.success(space);
    }

    /**
     * 根据空间id获取空间封装类
     * @param id id
     * @param request 请求
     * @return 空间封装类
     */
    @GetMapping("/get/vo")
    @ApiOperation(value = "根据id获取空间封装类")
    public BaseResponse<SpaceVO> getSpaceVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCodeEnum.PARAMS_ERROR);
        Space space = spaceService.getById(id);
        ThrowUtils.throwIf(space == null, ErrorCodeEnum.NOT_FOUND_ERROR);
        return ResultUtils.success(spaceService.getSpaceVO(space, request));
    }

    /**
     * 分页获取空间列表
     * @param spaceQueryRequest 查询请求
     * @return 分页结果
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstants.ADMIN_ROLE)
    @ApiOperation(value = "分页获取空间列表")
    public BaseResponse<Page<Space>> listSpaceByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        ThrowUtils.throwIf(spaceQueryRequest == null, ErrorCodeEnum.PARAMS_ERROR);
        long current = spaceQueryRequest.getCurrent();
        long size = spaceQueryRequest.getPageSize();
        Page<Space> spacePage = spaceService.page(new Page<>(current, size),
                spaceService.getQueryWrapper(spaceQueryRequest));
        return ResultUtils.success(spacePage);
    }

    /**
     * 分页获取空间列表封装类
     * @param spaceQueryRequest 查询请求
     * @param request 请求
     * @return 分页结果
     */
    @PostMapping("/list/vo/page")
    @ApiOperation(value = "分页获取空间列表封装类")
    public BaseResponse<Page<SpaceVO>> listSpaceVOByPage(@RequestBody SpaceQueryRequest spaceQueryRequest,
                                                         HttpServletRequest request) {
        long current = spaceQueryRequest.getCurrent();
        long size = spaceQueryRequest.getPageSize();
        Page<Space> spacePage = spaceService.page(new Page<>(current, size),
                spaceService.getQueryWrapper(spaceQueryRequest));
        return ResultUtils.success(spaceService.getSpaceVoPage(spacePage, request));
    }

    /**
     * 获取空间等级列表
     * @return
     */
    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevelVO>> listSpaceLevel() {
        List<SpaceLevelVO> spaceLevelVoList = Arrays.stream(SpaceLevelEnum.values())
                .map(spaceLevelEnum -> new SpaceLevelVO(
                        spaceLevelEnum.getValue(),
                        spaceLevelEnum.getText(),
                        spaceLevelEnum.getMaxCount(),
                        spaceLevelEnum.getMaxSize()
                )).collect(Collectors.toList());
        return ResultUtils.success(spaceLevelVoList);
    }
}
